<?php
/*
Plugin Name: 会员续费优惠
Plugin URI: #
Description: #
Author: #
Author URI: https://gitee.com/jiangye123/b2-plugins
Version: 1.0
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}


//b2/function.php 366行 替换
/*
function b2_get_option($where,$key,$circle_id = 0){
    return apply_filters('b2_get_option',B2\Modules\Settings\Main::get_option($where,$key),$where,$key,$circle_id);
}
*/

class b2_vip_preferential_price
{
    public static function init()
    {
        add_action('cmb2_admin_init',function(){
            $setting = new_cmb2_box( array(
                'id'           => 'b2_a_bulid_options_page',
                'object_types' => array( 'options-page' ),
                'option_key'      => 'b2_a_bulid',
                'tab_group'    => 'b2_a_options',
                'parent_slug'     => 'b2_main_options',
                'tab_title'    => __('会员续费设置','b2'),
                'menu_title'   => __('会员续费设置','b2'),
                'save_button'     => __( '保存', 'b2' ),
            ));
            $data = get_option('b2_normal_user');
            $data = isset($data['user_vip_group']) ? $data['user_vip_group'] : array();
            if(!empty($data)){
                foreach($data as $k => $v){
                    $setting->add_field( array(
                        'name'    => $v["name"].__( '续费要显示的名称', 'b2' ),
                        'id'      => 'b2_a_name_'.$k,
                        'desc'=> __( '可以加点提示用于与原价格区分，举个例子 ***会员（续费享受九折优惠） ，不写就显示原来的名称，原来显示'.$v["name"], 'b2' ),
                        'type'    => 'text',
                    ));
                    $setting->add_field( array(
                        'name'    => $v["name"].__( '续费价格', 'b2' ),
                        'id'      => 'b2_a_num_'.$k,
                        'desc'=> __( '续费价格，不修改就写原价，原价为'.$v["price"], 'b2' ),
                        'type'    => 'text',
                    ));
                }
            }
        },50,5);
        add_filter( 'b2_get_option', function($data,$where,$key){
            if ( $where=='normal_user' & $key=='user_vip_group') {
                $user_id = get_current_user_id();
                if ($user_id) {
                    $vip = get_user_meta($user_id,'zrz_vip',true);
                    $vip = (string)preg_replace('/\D/s','',$vip);
                    if (!self::check_is_vip($data,$user_id)) {
                        if(isset($data[$vip])){
                            $data[$vip]["price"] = isset(get_option('b2_a_bulid')['b2_a_num_'.$vip]) ? get_option('b2_a_bulid')['b2_a_num_'.$vip] : $data[$vip]["price"];
                            $data[$vip]["name"] = isset(get_option('b2_a_bulid')['b2_a_name_'.$vip]) ? get_option('b2_a_bulid')['b2_a_name_'.$vip] : $data[$vip]["name"];
                        }
                    }
                }
            }
            return $data;
        }, 50, 3);
    }
    public static function check_is_vip($vip_data,$user_id){
        $user_lv_time = get_user_meta($user_id,'zrz_vip_time',true);
        //获取结束时间
        $vip = get_user_meta($user_id,'zrz_vip',true);
        if(!$vip){
            return true;
        }
        $vip = (string)preg_replace('/\D/s','',$vip);
        $day = $vip_data[$vip];
        $day = $day['time'];
        //如果是永久会员
        if(isset($user_lv_time['end']) && (string)$user_lv_time['end'] === '0' && (int)$day === 0){
            return false;
        }
        //如果不是永久会员
        if(isset($user_lv_time['end']) && (string)$user_lv_time['end'] !== '0' && (int)$day !== 0){
            //检查是否过期
            if($user_lv_time['end'] < strtotime(date('Y-m-d H:i:s'))){
                delete_user_meta($user_id,'zrz_vip');
                delete_user_meta($user_id,'zrz_vip_time');
                return true;
            }
            return false;
        }
        $current = strtotime(date('Y-m-d H:i:s'));
        $end = strtotime(date('Y-m-d H:i:s',strtotime('+'.$day.' day')));
        //如果是永久会员，并且管理员重新设置了非永久会员
        if(isset($user_lv_time['end']) && (string)$user_lv_time['end'] === '0' && (int)$day !== 0){
            update_user_meta($user_id,'zrz_vip_time',array(
                'start'=>$current,
                'end'=>$end
            ));
            return false;
        }
    }
}
$a = b2_vip_preferential_price::init();
////////////////////////////////////////



// array(1) {
//     [0]=>
//     array(10) {
//       ["name"]=>
//       string(12) "永久会员"
//       ["color"]=>
//       string(7) "#dd3333"
//       ["price"]=>
//       string(4) "9999"
//       ["time"]=>
//       string(1) "0"
//       ["allow_read"]=>
//       string(1) "0"
//       ["allow_download"]=>
//       string(1) "1"
//       ["allow_download_count"]=>
//       string(2) "20"
//       ["allow_videos"]=>
//       string(1) "0"
//       ["count"]=>
//       string(1) "0"
//       ["user_role"]=>
//       array(3) {
//         [0]=>
//         string(7) "message"
//         [1]=>
//         string(4) "post"
//         [2]=>
//         string(7) "comment"
//       }
//     }
//   }